package com.magic.shop.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.magic.shop.constants.CommonConstants;
import com.magic.shop.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author zhangbo
 * @create 2024/10/8
 */
@Slf4j
@Configuration
public class LoginUtils {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static StringRedisTemplate dataTraceRepositoryStatic; // 创建静态引用类
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static String login_token_prefix=CommonConstants.USER_LOGIN_PREFIX;

    @PostConstruct
    public void init() {
        // 构造完成之后将自动注入的引用类实例赋值给静态应用类
        dataTraceRepositoryStatic = this.stringRedisTemplate;
    }



    public static User getUserInfo(String token) throws Exception {

        String userStr = dataTraceRepositoryStatic.opsForValue().get(CommonConstants.USER_LOGIN_PREFIX + token);

        User o = userStr == null ? null : JSONObject.parseObject(userStr, User.class);
        return o;


    }
}
